import { login, logout } from '@/api/user'
import { getToken, setToken, removeToken, setShow } from '@/utils/auth'
import router, { resetRouter } from '@/router'
import{creatrouter,permission} from '@/utils/index'
import {asyncRouters} from '@/router/index'
import { getShow } from './../../utils/auth';
import Cookies from 'js-cookie'
const getDefaultState = () => {
  return {
    accessToken: getToken(),
    name: '',
    avatar: '',
    roles:"",
    routes2:[],
    permission:[]
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES:(state, roles)=>{
    state.roles = roles
  },
  SET_ROUTES2:(state, routes2)=>{
    state.routes2 = routes2
  },
  SET_permission:(state, routes2)=>{
    state.permission = routes2
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { account, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ account: account, password: password }).then(response => {
        const { data } = response
        if(data){
          let show =''
          Cookies.set('show',show=true)
        
          localStorage.setItem('accessToken', data.accessToken)
          localStorage.setItem('buttonPermissionList', JSON.stringify(data.buttonPermissionList))
          // 用户信息
          localStorage.setItem('adminAccountVO', JSON.stringify(data.adminAccountVO) )
          // 存入vuex,后端返回的路由数据
          commit('SET_ROLES',data.adminAccountVO)
                data.permissionList.forEach(v => {
                    if( ['统计','设置','权限'].includes( v.menuName)){
                      // substring 提取字符串中两个指定下标之间的字符
                        v.permissionList = [ {  menuName:v.menuName, menuPath:v.menuPath.substring(1),menType:1 ,permissionList:v.permissionList  ,hasFlag:v.hasFlag } ]
                    }
                });
                var arr = creatrouter(data.permissionList , asyncRouters  )
                commit('SET_ROUTES2',arr)
                commit('SET_permission',permission(data.permissionList ))
                router.addRoutes(arr)

        }
        commit('SET_TOKEN', data.accessToken)
        setToken(data.accessToken)
        // 在这里设置 路由  
        // router.addRoutes( creatrouter(data.permissionList , asyncRouters  ) )
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 退出登录
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // 删除token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

